package persistence.dao;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.StringTokenizer;

import persistence.db.DataSet;
import persistence.db.DatabaseConnector;
import persistence.model.AbstractModel;
import persistence.model.AnneeEtude;
import persistence.model.Semestre;

public class AnneeEtudeDAO extends AbstractDAO {
	
	private AnneeEtude anneeEtude;
	
	/**
	 * Constructeur d'une année d'étude DAO
	 */
	public AnneeEtudeDAO() {
		setTableName("ANNEE_ETUDE");
	}
	
	/**
	 * Constructeur d'une année d'étude DAO
	 * @param annee Année d'étude correspondante
	 */
	public AnneeEtudeDAO(AnneeEtude annee) {
		this();
		anneeEtude = annee;
		setId(annee.getId());
	}
	
	public AnneeEtude getAnneEtude() {
		return anneeEtude;
	}

	public void setAnneeEtude(AnneeEtude anneEtude) {
		this.anneeEtude = anneEtude;
	}

	public DataSet all() {
		return new DataSet(this);
	}

	public AbstractModel makeModel(ResultSet rset) {
		try {
			setId(rset.getInt("id_Annee_Etude"));
			
			DataSet semestresSet = Semestre.all().filter("FK_SEMESTRE_AE = " + getId());
			
			String debut = dateToString(rset.getDate("debutAnnee"));
			String fin = dateToString(rset.getDate("finAnnee"));
			
			return new AnneeEtude(this.getId(), 
					rset.getString("nomAnneeEtude"), 
					rset.getString("descriptionAnneeEtude"), 
					debut, 
					fin, 
					rset.getInt("nbSeanceAnneeEtudeMatin"), 
					rset.getInt("nbSeanceAnneeEtudeAprem"), 
					rset.getInt("dureeSeance"), 
					rset.getInt("FK_AE_FORMATION"),
					semestresSet);
		} catch (SQLException e) {
			System.out.println("makeModel error");
			e.printStackTrace();
		}
		return null;
	}
	
	public void save() {
		String query;
		ResultSet result;
		query = "INSERT INTO " + getTableName() + " VALUES ( S_PK_" + getTableName() + ".nextval, '" + anneeEtude.getDebutAnneeEtude() + "', '" + anneeEtude.getFinAnneeEtude() +"', '"+ anneeEtude.getNomAnneeEtude() + "', '" + anneeEtude.getDescriptionAnneeEtude() + "', '" + anneeEtude.getNbSeancesAEMatin() + "', '" + anneeEtude.getNbSeancesAEAprem() + "', '" + anneeEtude.getDureeSeance() + "', '" + anneeEtude.getIdFormation() +"')";
		/* Execute la requete */
		result = DatabaseConnector.getInstance().execute(query);
		setId(this.getId()); /* Mise a jour de l'identifiant */
	}
	/* Pour les requetes : ' ' String ou Date */
	public void save(int id) {
		String query;
		ResultSet result;
		query = "UPDATE " + getTableName() +
			" SET debutAnnee = '" + anneeEtude.getDebutAnneeEtude() +
			"', finAnnee = '" + anneeEtude.getFinAnneeEtude() +
			"', nomAnneeEtude = '"+ anneeEtude.getNomAnneeEtude() +
			"', descriptionAnneeEtude = '" + anneeEtude.getDescriptionAnneeEtude() +
			"', nbSeanceAnneeEtudeMatin = " + anneeEtude.getNbSeancesAEMatin() +
			", nbSeanceAnneeEtudeAprem = " + anneeEtude.getNbSeancesAEAprem() +
			", dureeSeance = " + anneeEtude.getDureeSeance() +
			", fk_AE_formation = " + anneeEtude.getIdFormation() +
			" WHERE ID_"+ getTableName() + " = "+ id;
		/* Execute la requete */
		result = DatabaseConnector.getInstance().execute(query);
	}
}